package com.wslds.blog.action.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.wslds.base.dao.page.PageAdapter;
import com.wslds.base.util.BlogCons;
import com.wslds.base.util.FunctionUtil;
import com.wslds.base.util.SessionUtil;
import com.wslds.base.util.Strings;
import com.wslds.bean.Tag;
import com.wslds.bean.Taginst;
import com.wslds.bean.Type;
import com.wslds.bean.ext.ArticleExt;
import com.wslds.bean.ext.TaginstExt;
import com.wslds.blog.action.BaseArticleAction;

@SuppressWarnings("serial")
@Component("articleAdminAction")
@Scope("prototype")
public class ArticleAction extends BaseArticleAction {


	@SuppressWarnings("all")
	public String listAjax() {

		if (null == articleExt) {
			articleExt = new ArticleExt();
		}
		articleExt.setExtraOrderColumns(" id desc ");

		if (this.getHttpRequest().getQueryString() != null) {
			this.setQueryString(Strings.cutPageQueryString(this
					.getHttpRequest().getQueryString()));
		}

		Map<Long, List<TaginstExt>> tagMap = (Map<Long, List<TaginstExt>>) SessionUtil
				.getGlobe(BlogCons.APPLICATION.TAGINST);
		Map<Long, Type> typeMap = (Map<Long, Type>) SessionUtil
				.getGlobe(BlogCons.APPLICATION.TYPE);

		PageAdapter<ArticleExt> adapter = service.pageExt("article_select_ext",
				articleExt, getPage());
		this.put("adapter", adapter);

		for (ArticleExt ext : adapter.getData()) {
			ext.setTagList(tagMap.get(ext.getId()));
			ext.setType(typeMap.get(ext.getTypeId()));

			String pureData = FunctionUtil.Html2Text(ext.getContent());

			int blogStart = ext.getContent().indexOf(BlogCons.BLOG_SPLIT.START);
			int blogEnd = ext.getContent().indexOf(BlogCons.BLOG_SPLIT.END);
			if (blogStart == -1 || blogEnd == -1) {
				ext.setContent(FunctionUtil.abbreviate(pureData, 500, "..."));
			} else {
				ext.setContent(ext.getContent().substring(blogStart, blogEnd)
						+ "........");
			}
		}

		return SUCCESS;
	}


	public String insert() {
		instance.setStat(BlogCons.BLOG_STAT.MEI);
		instance.setViewSum(0L);
		String result = super.insert();

		if (null != taginst) {
			for (Taginst inst : taginst) {
				if (0 != inst.getTagId()) {
					inst.setArtId(instance.getId());
					service.save("taginst", inst);
				}
			}
		}

		SessionUtil.refreshTag();
		SessionUtil.refreshSidebar();

		return result;
	}

	public String add() {
		List<Type> typeList = service.find("type", null);
		this.put("typeList", typeList);

		List<Tag> tagList = service.find("tag", null);
		this.put("tagList", tagList);

		return SUCCESS;
	}

	@SuppressWarnings("all")
	public String edit() {
		String result = super.edit();

		Map<Long, List<TaginstExt>> tagMap = (Map<Long, List<TaginstExt>>) SessionUtil
				.getGlobe(BlogCons.APPLICATION.TAGINST);
		List<TaginstExt> taginstList = tagMap.get(instance.getId());
		if (null == taginstList) {
			taginstList = new ArrayList<TaginstExt>();
			taginstList.add(new TaginstExt());
			taginstList.add(new TaginstExt());
			taginstList.add(new TaginstExt());
		} else {
			for (int i = taginstList.size(); i < 3; i++) {
				taginstList.add(new TaginstExt());
			}
		}
		this.put("taginstList", tagMap.get(instance.getId()));

		List<Type> typeList = service.find("type", null);
		this.put("typeList", typeList);

		List<Tag> tagList = service.find("tag", null);
		this.put("tagList", tagList);

		return result;
	}

	public String update() {
		String result = super.update();
		Taginst delTaginst = new Taginst();
		delTaginst.setArtId(instance.getId());
		service.delete("taginst", delTaginst);
		if (null != taginst) {
			for (Taginst inst : taginst) {
				if (0 != inst.getTagId()) {
					inst.setArtId(instance.getId());
					service.save("taginst", inst);
				}
			}
		}
		SessionUtil.refreshTag();
		SessionUtil.refreshSidebar();
		return result;
	}

	@SuppressWarnings("all")
	public String show() {

		if (Strings.notNull(articleExt.getIds())) {

			try {
				articleExt.setId(Long.valueOf(articleExt.getIds()));
			} catch (Exception e) {
				// 要处理
				return REDRICT_TO_LIST;
			}

			articleExt = service.findOneExt("article_select_ext", articleExt);

			String pureData = FunctionUtil.Html2Text(articleExt.getContent());
			articleExt.setContent4Simple(FunctionUtil.abbreviate(pureData, 150,
					"..."));

			Map<Long, List<TaginstExt>> tagMap = (Map<Long, List<TaginstExt>>) SessionUtil
					.getGlobe(BlogCons.APPLICATION.TAGINST);
			Map<Long, Type> typeMap = (Map<Long, Type>) SessionUtil
					.getGlobe(BlogCons.APPLICATION.TYPE);
			articleExt.setTagList(tagMap.get(articleExt.getId()));
			articleExt.setType(typeMap.get(articleExt.getTypeId()));

			List<ArticleExt> neiList = service.getNei(articleExt);
			this.put("neiList", neiList);

		} else {
			// 错误去处
			return REDRICT_TO_LIST;
		}

		return SUCCESS;
	}

}
